#include <stdio.h>
#include <stdlib.h>

#define MaxVertexNum 10

/* 完整版 */
typedef int Vertex;
typedef int WeightType;
typedef int DataType;
struct GNode{
    int Nv;
    int Ne;
    WeightType G[MaxVertexNum][MaxVertexNum];
    // DataType Data[MaxVertexNum];
};
typedef struct GNode* PtrToGNode;
typedef PtrToGNode MGraph;

/* 初始化一个有MaxVertexNum个顶点但没有边的图 */
MGraph CreateGraph(int VertexNum)
{
    MGraph Graph;
    Vertex v, w;
    Graph = (MGraph)malloc(sizeof(struct GNode));
    Graph->Nv = VertexNum;
    Graph->Ne = 0;
    for (v = 0; v < Graph->Nv; v++)
        for (w = 0; w < Graph->Nv; w++)
            Graph->G[v][w] = 0;
    return Graph;
}

/* 定义边结点 */
struct ENode{
    Vertex v1, v2;
    WeightType weight;
};
typedef struct ENode* PtrToENode;
typedef PtrToENode Edge;

/* 向图中插入边 */
void InsertEdge(MGraph Graph, Edge E)
{
    Graph->G[E->v1][E->v2] = E->weight;
    /* 若是无向图，还要插入边<v2, v1> */
    Graph->G[E->v2][E->v1] = E->weight;
}

/* 建立图，输入如下：
 * Nv Ne
 * v1 v2 weight
 * ... ...
 */
MGraph BuildGraph()
{
    MGraph Graph;
    Edge E;
    int Nv, i;
    scanf("%d", &Nv);
    Graph = CreateGraph(Nv);
    scanf("%d", &(Graph->Ne));
    if (Graph->Ne != 0) {
        E = (Edge)malloc(sizeof(struct ENode));
        for (i = 0; i < Graph->Ne; i++) {
            scanf("%d %d %d", &E->v1, &E->v2, &E->weight);
            InsertEdge(Graph, E);
        }
    }
    /* 如果顶点有数据的话，读入数据 */
    // for (Vertex v = 0; v < Graph->Nv; v++)
        // scanf("%d", &Graph->Data[v]);
    return Graph;
}

/* 更简单的方法创建图 */
void BuildGraphSimple(int G[MaxVertexNum][MaxVertexNum])
{
    int Nv, Ne, v, w, weight, i, j;
    scanf("%d %d", &Nv, &Ne);
    for (i = 0; i < Nv; i++)
        for (j = 0; j < Nv; j++)
            G[i][j] = 0;
    for (i = 0; i < Ne; i++) {
        scanf("%d %d %d", &v, &w, &weight);
        G[v][w] = weight;
        G[w][v] = weight;
    }
}

int main()
{
    int i, j;
    MGraph Graph = BuildGraph();
    int G[MaxVertexNum][MaxVertexNum];
    BuildGraphSimple(G);
    for (i = 0; i < MaxVertexNum; i++) {
        for (j = 0; j < MaxVertexNum; j++)
            printf("%3d", G[i][j]);
        printf("\n");
    }
    for (i = 0; i < MaxVertexNum; i++) {
        for (j = 0; j < MaxVertexNum; j++)
            printf("%3d", Graph->G[i][j]);
        printf("\n");
    }
}